<?php

require_once(dirname(__FILE__).'/Base.php');

class DB_Member extends DB_Base {

    public function insert_member ($member) {
        $member['displayName']  = sprintf('%s', $member['displayName']);
        $member['name']         = sprintf('%s', $member['name']);
        $member['nickname']     = sprintf('%s', $member['nickname']);
        $member['thumbnailUrl'] = sprintf('%s', $member['thumbnailUrl']);
        $sth = $this->dbh->prepare('
            REPLACE INTO
                member_info
            SET
                member_id     = ?,
                display_name  = ?,
                name          = ?,
                nickname      = ?,
                thumbnail_url = ?
        ');
        if (!$sth) throw new Exception($this->dbh->error);
        $sth->bind_param('dssss',
            $member['id'],
            $member['displayName'],
            $member['name'],
            $member['nickname'],
            $member['thumbnailUrl']
        );
        $sth->execute();
        return $sth->rows;
    }

    public function select_member ($member_id_list, $expired) {
        if (!is_array($member_id_list)) {
            $member_id_list = array($member_id_list);
        }
        foreach ($member_id_list as &$member_id) {
            is_numeric($member_id) or die;
            $member_id = "'$member_id'";
        }

        $member_id_in = implode(', ', $member_id_list);

        $sth = $this->dbh->prepare("
            SELECT
                member_id,
                display_name,
                name,
                nickname,
                thumbnail_url
            FROM
                member_info
            WHERE
                last_update > ? AND
                member_id IN ($member_id_in)
        ");
        if (!$sth) throw new Exception($this->dbh->error);
        $sth->bind_param('s', $expired);
        $sth->execute();
        $sth->bind_result(
            $member_id,
            $display_name,
            $name,
            $nickname,
            $thumbnail_url
        );

        $rows = array();
        while ($sth->fetch()) {
            $rows[] = array(
                'id'           => $member_id,
                'displayName'  => $display_name,
                'name'         => $name,
                'nickname'     => $nickname,
                'thumbnailUrl' => $thumbnail_url,
            );
        }
        return $rows;
    }

    public function insert_link ($member_id, $linked_member_id) {
        $sth = $this->dbh->prepare('
            REPLACE INTO
                member_link
            SET
                member_id        = ?,
                linked_member_id = ?
        ');
        if (!$sth) throw new Exception($this->dbh->error);
        $sth->bind_param('ds', $member_id, $linked_member_id);
        $sth->execute();
        return;
    }

    public function select_link ($member_id, $expired) {
        $sth = $this->dbh->prepare('
            SELECT
                linked_member_id
            FROM
                member_link
            WHERE
                member_id   = ? AND
                last_update > ?
        ');
        if (!$sth) throw new Exception ($this->dbh->error);
        $sth->bind_param('ds', $member_id, $expired);
        $sth->execute();
        $sth->bind_result($linked_member_id);

        if ($sth->fetch()) {
            return $linked_member_id;
        }
        return;
    }

    public function insert_link_optin ($member_id, $linked_member_id) {
        $sth = $this->dbh->prepare('
            REPLACE INTO
                member_link_optin
            SET
                member_id        = ?,
                linked_member_id = ?
        ');
        if (!$sth) throw new Exception($this->dbh->error);
        $sth->bind_param('ds', $member_id, $linked_member_id);
        $sth->execute();
        return;
    }

    public function select_link_optin ($member_id, $expired) {
        $sth = $this->dbh->prepare('
            SELECT
                linked_member_id
            FROM
                member_link_optin
            WHERE
                member_id   = ? AND
                last_update > ?
        ');
        if (!$sth) throw new Exception ($this->dbh->error);
        $sth->bind_param('ds', $member_id, $expired);
        $sth->execute();
        $sth->bind_result($linked_member_id);

        if ($sth->fetch()) {
            return $linked_member_id;
        }
        return;
    }
}
